package advanced;

import java.io.BufferedInputStream;
import java.io.BufferedOutputStream;
import java.io.DataInputStream;
import java.io.DataOutputStream;
import java.io.FileInputStream;
import java.io.FileOutputStream;
import java.io.FilterOutputStream;
import java.io.InputStream;
import java.io.ObjectInputStream;
import java.io.ObjectOutputStream;
import java.io.OutputStream;
import java.util.ArrayList;
import java.util.List;

import org.junit.Test;

public class Test1 {
	@Test
	public void test1() throws Exception{
		long start=System.currentTimeMillis();
		InputStream in=new FileInputStream("jeep1.jpg");
		OutputStream out=new FileOutputStream("jeep2.jpg");
		int m=0;
		while((m=in.read())!=-1){
			out.write((byte)m);
		}
		in.close();
		out.close();
		long end=System.currentTimeMillis();
		System.out.println(end-start);
	}
	@Test
	public void test2() throws Exception{
		long start=System.currentTimeMillis();
		InputStream in=new FileInputStream("jeep1.jpg");
		OutputStream out=new FileOutputStream("jeep2.jpg");
		int m=0;
		byte[] buf=new byte[64];
		while((m=in.read(buf))!=-1){
			out.write(buf,0,m);
		}
		in.close();
		out.close();
		long end=System.currentTimeMillis();
		System.out.println(end-start);
	}
	
	@Test
	public void test3() throws Exception{
		byte[] info="john".getBytes();
		long start=System.currentTimeMillis();
		InputStream in=new FileInputStream("jeep1.jpg");
		OutputStream out=new FileOutputStream("jeep2.jpg");
		int m=0;
		byte[] buf=new byte[64];
		int index=0;
		int count=0;
		while((m=in.read(buf))!=-1){
			out.write(buf,0,m);
			if(count>8){
				if(index<info.length) out.write(info[index++]);
			}
			count++;
		}
		in.close();
		out.close();
		long end=System.currentTimeMillis();
		System.out.println(end-start);
	}
	
	
	@Test
	public void test4() throws Exception{
		
		byte[] bs=new byte[4];
		InputStream in=new FileInputStream("jeep2.jpg");
		int m=0;
		byte[] buf=new byte[64];
		int index=0;
		int count=0;
		while((m=in.read(buf))!=-1){
			if(count>8&&index<4){
				byte b=(byte)in.read();
				bs[index++]=b;
				
			}
			count++;
		}
		
		
		in.close();
		System.out.println(new String(bs));
	}
	
	@Test
	public void test5() throws Exception{
		double d=34.5678;
		OutputStream out=new FileOutputStream("data2.txt");
		DataOutputStream dout=new DataOutputStream(out);
		dout.writeDouble(d);
		dout.writeChars("abc");
		dout.close();
		//out.close();
	}
	
	@Test
	public void test6() throws Exception{
		
		InputStream in=new FileInputStream("data2.txt");
		DataInputStream din=new DataInputStream(in);
		double d=din.readDouble();
		System.out.println(d);
		din.close();
		//out.close();
	}
	
	@Test
	public void test8() throws Exception{
		long start=System.currentTimeMillis();
		BufferedInputStream in=new BufferedInputStream(new FileInputStream("jeep1.jpg"));
		BufferedOutputStream out=new BufferedOutputStream(new FileOutputStream("jeep3.jpg"));
		int m=0;
		while((m=in.read())!=-1){
			out.write((byte)m);
		}
		in.close();
		out.close();
		long end=System.currentTimeMillis();
		System.out.println(end-start);
	}
	
	
	@Test
	public void test9() throws Exception{
		long start=System.currentTimeMillis();
		DataOutputStream dout=new DataOutputStream(new BufferedOutputStream(new FileOutputStream("data4.data")));
		for(int i=0;i<1000000;i++){
			int m=(int)(Math.random()*1001);
			dout.writeInt(m);
		}
		dout.close();
		long end=System.currentTimeMillis();
		System.out.println(end-start);
	}
	
	@Test
	public void test10() throws Exception{
		Customer c=new Customer();
		c.setId(100);
		c.setName("john");
		c.setAge(101);
		ObjectOutputStream out=new ObjectOutputStream(new FileOutputStream("data5.data"));
		out.writeObject(c);
		out.close();
	}
	
	@Test
	public void test11() throws Exception{
		ObjectInputStream in=new ObjectInputStream(new FileInputStream("data5.data"));
		Customer c=(Customer)in.readObject();
		System.out.println(c.getId()+"...."+c.getName()+"...."+c.getAge());
	}
	
	
	
	
	
	
	
	
	
	
	
	
	
	
	public static void main(String[] args) {

	}

}
